<?php

use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Config;
use Fisharebest\Webtrees\Functions\FunctionsEdit;
use Fisharebest\Webtrees\GedcomTag;
use Fisharebest\Webtrees\Http\RequestHandlers\EditFactAction;
use Fisharebest\Webtrees\I18N;
use Ramsey\Uuid\Uuid;

?>

<h2 class="wt-page-title"><?= $title ?></h2>

<form method="post" action="<?= e(route(EditFactAction::class, ['tree' => $tree->name(), 'xref' => $record->xref()])) ?>" class="wt-page-content">
    <?= csrf_field() ?>

    <?php FunctionsEdit::createAddForm($tree, $fact) ?>

    <?php if ($record->tag() === 'SOUR' && $fact === 'DATA') : ?>
        <?= view('cards/add-note', ['level' => 2, 'tree' => $tree]) ?>
        <?= view('cards/add-shared-note', ['level' => 2, 'tree' => $tree]) ?>
    <?php endif ?>

    <?php if (($record->tag() === 'INDI' || $record->tag() === 'FAM') && $fact !== 'OBJE' && $fact !== 'NOTE' && $fact !== 'SHARED_NOTE' && $fact !== 'REPO' && $fact !== 'SOUR' && $fact !== 'SUBM' && $fact !== 'ASSO' && $fact !== 'ALIA' && $fact !== 'SEX') : ?>
        <?= view('cards/add-source-citation', [
            'level'          => 2,
            'full_citations' => $tree->getPreference('FULL_SOURCES'),
            'tree'           => $tree,
        ]) ?>

        <?php if ($tree->getPreference('MEDIA_UPLOAD') >= Auth::accessLevel($tree)) : ?>
            <?= view('cards/add-media-object', [
            'level' => 2,
            'tree' => $tree,
            ]) ?>
        <?php endif ?>

        <?php if ($fact !== 'NOTE') : ?>
            <?= view('cards/add-note', [
                'level' => 2,
                'tree' => $tree,
            ]) ?>

            <?= view('cards/add-shared-note', [
                'level' => 2,
                'tree' => $tree,
            ]) ?>
        <?php endif ?>

        <?= view('cards/add-associate', [
            'id'    => Uuid::uuid4()->toString(),
            'level' => 2,
            'tree' => $tree,
        ]) ?>
        <?php if (in_array($fact, Config::twoAssociates(), true)) : ?>
            <?= view('cards/add-associate', [
                'id'    => Uuid::uuid4()->toString(),
                'level' => 2,
                'tree' => $tree,
            ]) ?>
        <?php endif ?>

        <?= view('cards/add-restriction', [
            'level' => 2,
            'tree' => $tree,
        ]) ?>
    <?php endif ?>

    <div class="form-group row">
        <label class="col-sm-3 col-form-label" for="keep_chan">
            <?= I18N::translate('Last change') ?>
        </label>
        <div class="col-sm-9">
            <?= view('components/checkbox-inline', ['label' => I18N::translate('Keep the existing “last change” information'), 'name' => 'keep_chan', 'checked' => (bool) $tree->getPreference('NO_UPDATE_CHAN')]) ?>
            <?= GedcomTag::getLabelValue('DATE', view('components/datetime', ['timestamp' => $record->lastChangeTimestamp()])) ?>
            <?= GedcomTag::getLabelValue('_WT_USER', e($record->lastChangeUser())) ?>
        </div>
    </div>

    <div class="form-group row">
        <div class="col-sm-3 wt-page-options-label">
        </div>
        <div class="col-sm-9 wt-page-options-value">
            <button class="btn btn-primary" type="submit">
                <?= view('icons/save') ?>
                <?= /* I18N: A button label. */
                I18N::translate('save') ?>
            </button>
            <a class="btn btn-secondary" href="<?= e($record->url()) ?>">
                <?= view('icons/cancel') ?>
                <?= /* I18N: A button label. */
                I18N::translate('cancel') ?>
            </a>
        </div>
    </div>
</form>

<?= view('modals/on-screen-keyboard') ?>
<?= view('modals/ajax') ?>
<?= view('edit/initialize-calendar-popup') ?>
